package com.example.hls_homework3;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvEventInfo;

    private int[] data = new int[100];
    int hasData = 0;
    final int PROGRESS_DIALOG = 0x112;
    int progressStatus = 0;
    ProgressDialog pd;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvEventInfo = findViewById(R.id.tv_event_info);

        // 内部类监听
        Button btnInnerClass = findViewById(R.id.btn_inner_class);
        btnInnerClass.setOnClickListener(new InnerClickListener());

        // 匿名类监听
        Button btnAnonymousClass = findViewById(R.id.btn_anonymous_class);
        btnAnonymousClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvEventInfo.setText("使用匿名类处理点击事件");
            }
        });

        // Activity作为监听器
        Button btnActivityListener = findViewById(R.id.btn_activity_listener);
        btnActivityListener.setOnClickListener(this);

        // Lambda表达式监听
        Button btnLambda = findViewById(R.id.btn_lambda);
        btnLambda.setOnClickListener(v -> tvEventInfo.setText("使用Lambda表达式处理点击事件"));

        Button system_info = findViewById(R.id.system_info);
        system_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SystemInfoActivity.class);
                startActivity(intent);
            }
        });

        Button execBn = findViewById(R.id.exec);
        execBn.setOnClickListener(source -> showDialog(PROGRESS_DIALOG));

        // 初始化Handler
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0x123) {
                    pd.setProgress(progressStatus);
                }
            }
        };
    }

    // 内部类
    class InnerClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            tvEventInfo.setText("使用内部类处理点击事件");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_activity_listener) {
            tvEventInfo.setText("使用Activity作为监听器处理点击事件");
        }
    }

    @Override
    public Dialog onCreateDialog(int id) {
        System.out.println("------create------");
        switch (id) {
            case PROGRESS_DIALOG:
                pd = new ProgressDialog(this);
                pd.setMax(100);
                pd.setTitle("任务完成百分比");
                pd.setMessage("耗时任务的完成百分比");
                pd.setCancelable(false);
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd.setIndeterminate(false);
                break;
        }
        return pd;
    }

    @Override
    public void onPrepareDialog(int id, Dialog dialog) {
        System.out.println("------prepare------");
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case PROGRESS_DIALOG:
                pd.incrementProgressBy(-pd.getProgress());
                new Thread() {
                    public void run() {
                        while (progressStatus < 100) {
                            progressStatus = doWork();
                            // 发送消息到Handler
                            handler.sendEmptyMessage(0x123);
                        }
                        if (progressStatus >= 100) {
                            pd.dismiss();
                        }
                    }
                }.start();
                break;
        }
    }

    public int doWork() {
        data[hasData++] = (int) (Math.random() * 100);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hasData;
    }
}